﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;  

namespace TimKen.Web.User
{
    public partial class MessageView : TimKen.App.WebPage
    {
        TimKen.BussLogic.InboxManager inboxM = new TimKen.BussLogic.InboxManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentUserName == null || CurrentUserName == "")
                {
                    Alert("您必须登录！", "../Index.aspx");
                }
                if (Request.QueryString["MsgID"] != null)
                {
                    MsgID = Request.QueryString["MsgID"];
                    BindMsg();
                }
                BindResources();
            }
        }
        protected string MsgID
        {
            get
            {
                if (ViewState["MsgID"] != null)
                    return (ViewState["MsgID"].ToString());
                else
                    return "";
            }
            set
            {
                ViewState["MsgID"] = value;
            }
        }

        protected void BindMsg()
        {
            TimKen.DataEntity.Inbox msg = inboxM.GetModel(int.Parse(MsgID));
            if (msg == null)
            {
                Response.Redirect("/ErrorAuthority.aspx");
            }
            else if (msg.ToID != CurrentUserName && msg.FromID != CurrentUserName)
            {
                Response.Redirect("/ErrorAuthority.aspx");
            }
            else
            {
                if (!msg.IsRead)
                {
                    msg.IsRead = true;
                    inboxM.Update(msg);
                }
                labCreateDate.Text = msg.CreateDate.Value.ToShortDateString();
                labFromID.Text = msg.FromID;
                labToID.Text = msg.ToID;
                labMessage.Text = msg.Message.Replace("\n", "</br>");
                labTitle.Text = msg.Title;
                txtToID.Text = msg.FromID;
                txtTitle.Text = GetLangString("ReplyMessage") + "：" + msg.Title;
                txtMessage.Text = "\n\n\n----------------------------------------------------\n";
                txtMessage.Text += ">>" + GetLangString("MessageFrom") + "：" + msg.FromID + "\n";
                txtMessage.Text += ">>" + GetLangString("MessageTitle") + "：" + msg.Title + "\n";
                txtMessage.Text += ">>" + GetLangString("MessageCreateDate") + "：" + msg.CreateDate.Value.ToShortDateString() + "\n";
                txtMessage.Text += msg.Message + "\n";
                txtMessage.Text += "----------------------------------------------------\n";
            }
        }

        protected void BindResources()
        {
            btnSubmit.Text = GetLangString("ReplyMessage");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            TimKen.DataEntity.Inbox msg = new TimKen.DataEntity.Inbox();
            msg.FromID = CurrentUserName;
            msg.Message = txtMessage.Text;
            msg.Title = txtTitle.Text;
            msg.ToAll = 0;
            msg.ToID = txtToID.Text;
            inboxM.Add(msg);
            Response.Redirect("/User/Inbox.aspx");
        }
    }
}
